<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" placeholder="请选择" clearable @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="queryParams.projectName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="审批状态" prop="approveStatus">
            <el-select v-model="queryParams.approveStatus" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in appStatusOptions" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="申请人" prop="staffName">
            <el-input v-model="queryParams.staffName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="materialAppList" border>
      <el-table-column label="项目区域" align="center" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion" />
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="申请人" align="center" prop="staffName" />
      <el-table-column label="联系方式" align="center" prop="staffPhone" />
      <el-table-column label="申请时间" align="center" prop="appTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请总数量" align="center" prop="totalNumber" />
      <el-table-column label="申请总成本" align="center" prop="totalMoney">
        <template slot-scope="scope">
          {{ toMoney(scope.row.totalMoney) }}
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="approveStatus">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.approveStatus == 0">待审批</el-tag>
          <el-tag type="success" v-if="scope.row.approveStatus == 1">已通过</el-tag>
          <el-tag type="danger" v-if="scope.row.approveStatus == 2">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="调拨单号" align="center" prop="meaterialOdd" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="scope.row.approveStatus == 0" @click="handleAuditOrView(scope.row, 1)" v-hasPermi="['cld:materialApp:audit']">审核</el-button>
          <el-button size="mini" type="text" @click="handleAuditOrView(scope.row, 2)" v-hasPermi="['cld:materialApp:query']">查看明细</el-button>
          <!--<el-button size="mini" type="text" @click="handleDelete(scope.row)" v-hasPermi="['cld:materialApp:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 查看明细or审核 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-table v-loading="loading" :data="form.detailsList" border>
          <el-table-column label="商品类别" align="center" prop="commodityType" />
          <el-table-column label="商品编号" align="center" prop="commodityNo" />
          <el-table-column label="商品名称" align="center" prop="commodityName1" />
          <el-table-column label="规格" width="80" align="center" prop="commoditySpec" />
          <el-table-column label="单位" width="150" align="center" prop="unitName1" />
          <el-table-column label="申请数量" align="center" prop="appNumber" />
          <el-table-column label="单价" width="120" align="center" prop="costPrice">
            <template slot-scope="scope">
              {{ toMoney(scope.row.costPrice) }}
            </template>
          </el-table-column>
          <el-table-column label="总价" width="120" align="center" prop="sumMoney">
            <template slot-scope="scope">
              {{ toMoney(scope.row.sumMoney) }}
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="type == 1" @click="handleAuditOptions(1)">通过</el-button>
        <el-button type="primary" v-if="type == 1" @click="handleAuditOptions(2)">驳回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterialApp, getMaterialApp, delMaterialApp, auditMaterialApp } from "@/api/cld/materialApp";
import {appStatusOptions} from '@/assets/staticDict'

export default {
  name: "MaterialApp",
  dicts: ['cld_project_region'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 项目申请表格数据
      materialAppList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态
      appStatusOptions: appStatusOptions,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegion: null,
        projectName: null,
        staffName: null,
        approveStatus:null,
      },
      // 表单参数
      form: {},
      type: 2,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目申请列表 */
    getList() {
      this.loading = true;
      listMaterialApp(this.queryParams).then(response => {
        this.materialAppList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materialAppId: null,
        staffId: null,
        projectId: null,
        appTime: null,
        meaterialOdd: null,
        appStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 审核or详情按钮操作 */
    handleAuditOrView(row, type) {
      this.reset();
      this.type = type;
      getMaterialApp(row.materialAppId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = row.projectName + '-申请明细';
      });
    },
    /** 提交按钮 */
    handleAuditOptions(approveStatus) {
      this.form.approveStatus = approveStatus;
      if (approveStatus == 2) {
        this.$prompt('', '驳回原因', {
          center: true,
          inputType: 'textarea',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          this.form.rejectReason = value;
          this.submitForm();
        })
      } else {
        this.submitForm();
      }
    },
    submitForm() {
      const loading = this.$loading({
        lock: true,
        text: '审核中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      auditMaterialApp(this.form).then(response => {
        loading.close();
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialAppIds = row.materialAppId || this.ids;
      this.$modal.confirm('是否确认删除项目申请编号为"' + materialAppIds + '"的数据项？').then(function () {
        return delMaterialApp(materialAppIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
